﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Av.Utils;
using System.Xml.XPath;
using System.IO;
using System.Diagnostics;

namespace WebUtils
{
    public class SymI18nXmlParser
    {

        protected Dictionary<string, string> m_arI18n = new Dictionary<string, string>();

        /// <summary>
        /// 
        /// </summary>
        protected Dictionary<string, List<string>> m_arDuplicated = new Dictionary<string, List<string>>();

        public string ModuleName { get; set; }

        public string CurrentCulture { get; set; }

        public void Parse(string filename)
        {
            try
            {
                Log4cs.Log("Parsing symfony I18N file: {0}...", filename);
                XPathDocument doc = new XPathDocument(File.OpenRead(filename));
                XPathNavigator navigator = doc.CreateNavigator();
                XPathNodeIterator iterator = navigator.Select("/xliff/file/body/trans-unit");
                Log4cs.Log("Total trans-units: {0}", iterator.Count);

                Reset();

                Stopwatch sw = new Stopwatch();
                sw.Start();

                while(iterator.MoveNext())
                {
                    string key = "";
                    string translation = "";

                    XPathNodeIterator children = iterator.Current.SelectChildren(XPathNodeType.Element);
                    while(children.MoveNext())
                    {
                        if(children.Current.Name == "source")
                        {
                            key = children.Current.Value;
                        } else if(children.Current.Name == "target")
                        {
                            translation = children.Current.Value;
                        }
                        //Log4cs.Log("  {0}", children.Current.Name);
                    }
                    //Log4cs.Log("{0}: {1}", key, translation);

                    if( !string.IsNullOrEmpty(key) )
                    {
                        if(!m_arI18n.ContainsKey(key))
                        {
                            // Just add if it is first translation
                            m_arI18n[key] = translation;
                        } else if(string.IsNullOrEmpty(m_arI18n[key]))
                        {
                            // Overwrite w/o translation
                            m_arI18n[key] = translation;
                        } else
                        {
                            // Put duplicate in list of variants
                            if( !m_arDuplicated.ContainsKey(key) )
                            {
                                m_arDuplicated[key] = new List<string>();
                                m_arDuplicated[key].Add(m_arI18n[key]);
                            }
                            m_arDuplicated[key].Add(translation);
                        }
                    }

                }  // END WHILE ( loop through all translations )
                sw.Stop();
                Log4cs.Log("Parsed in {0} ticks", sw.ElapsedTicks);

            } catch(Exception ex)
            {
                Log4cs.Log(Importance.Error, "Error parsing XML file {0}!", filename);
                Log4cs.Log(Importance.Debug, ex.ToString());
            }
        }

        public void Reset()
        {
            CurrentCulture = "";
            ModuleName = "";
            m_arI18n.Clear();
        }


        public Dictionary<string, string> GetTranslations()
        {
            return m_arI18n;
        }
    }
}
